﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Repositories
{

    public class CommentRepository
    {
        gsDBDataContext link = new gsDBDataContext();

        /// <summary>
        /// Adding a comment to some content 
        /// Precondition: Some content has to be added and comments activated by teacher/admin
        /// Postcondition: A comment has been added
        /// Author: Bjarmi/Aron mod. by Alma
        /// Date: 04.05. 2012
        /// </summary>
        public int addComment(Comment comment)
        {
            try
            {
                link.Comments.InsertOnSubmit(comment);
                save();
            }
            catch (Exception)
            {
                return -1;
            }
            return comment.ID; 
        }

        /// <summary>
        /// Getting all comments for content
        /// Precondition: Comments have been submitted
        /// Postcondition: A list of comments are returned
        /// Author: Bjarmi/Aron mod. by Alma
        /// Date: 04.05. 2012
        /// </summary>
        public IQueryable<Comment> getAllcomments()
        {
            return link.Comments;
        }

        /// <summary>
        /// Getting a comment based on an ID
        /// Precondition: A comment has been submitted and has an ID
        /// Postcondition: A comment is returned
        /// Author: Bjarmi mod. by Alma
        /// Date: 04.05. 2012
        /// </summary>
        public Comment getCommentByID(int commentID)
        {
            Comment selectedComment = (from c in link.Comments
                                        where c.ID == commentID
                                        select c).First();
            return selectedComment; 
        }
        /// <summary>
        /// Editing comments
        /// Precondition: A comment has been submitted and a user has a right to edit it.
        /// Postcondition: The comment has been edited and saved again
        /// Author: Aron mod. by Alma
        /// Date: 04.05. 2012
        /// </summary>
        public bool editComment(Comment myComment)
        {
            try
            {
                var comm = (from c in link.Comments
                            where c.authorID == myComment.authorID
                            select c).First();

                comm.content = myComment.content;

                save();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }


        //SPECIALIZD GETZnSETZ
        //course comments

        public IQueryable<Comment> getCourseComment(int courseID)
        {
            var result = from c in link.Comments
                         from cc in link.CourseComments
                         where cc.courseID == courseID && c.ID == cc.commentID
                         orderby c.ID descending
                         select c;
            return result;
        }
        public int addCourseComment(int courseID, Comment c)
        {
            int commentID = addComment(c);
            CourseComment cc = new CourseComment();
            cc.courseID = courseID;
            cc.commentID = commentID;
            link.CourseComments.InsertOnSubmit(cc);
            save();
            return cc.commentID;
        }
        
        //AssComments
        public IQueryable<Comment> getAssComments(int assID)
        {
            var result = from c in link.Comments
                         from ac in link.AssignmentComments
                         where ac.assID == assID && c.ID == ac.commentID
                         orderby c.ID descending
                         select c;
            return result;
        }
        public int addAssComment(int assID, Comment c)
        {
            int commentID = addComment(c);
            AssignmentComment assComment = new AssignmentComment();
            assComment.assID = assID;
            assComment.commentID = commentID;
            link.AssignmentComments.InsertOnSubmit(assComment);
            save();
            return assComment.commentID;
        }

        //MatComments
        public IQueryable<Comment> getMaterialComments(int matID)
        {
            var result = from c in link.Comments
                         from mc in link.MaterialComments
                         where mc.matID == matID && c.ID == mc.commentID
                         orderby c.ID descending
                         select c;
            return result;
        }
        public int addMaterialComment(int matID, Comment c)
        {
            int commentID = addComment(c);
            MaterialComment mc = new MaterialComment();
            mc.matID = matID;
            mc.commentID = commentID;
            link.MaterialComments.InsertOnSubmit(mc);
            save();
            return mc.commentID;
        }

        //LectureComments
        public IQueryable<Comment> getLectureComments(int lectureID)
        {
            var result = from c in link.Comments
                         from lc in link.LectureComments
                         where lc.lectureID == lectureID && c.ID == lc.commentID
                         orderby c.ID descending
                         select c;
            return result;
        }
        public int addLectureComment(int lectureID, Comment c)
        {
            int commentID = addComment(c);
            LectureComment lc = new LectureComment();
            lc.lectureID = lectureID;
            lc.commentID = commentID;
            link.LectureComments.InsertOnSubmit(lc);
            save();
            return lc.commentID;
        }

        public void removeComment(int commentID)
        {
            Comment comm = (from c in link.Comments
                            where c.ID == commentID
                            select c).First();

            CourseComment cc = (from c in link.CourseComments
                                where c.commentID == commentID
                                select c).First();
            AssignmentComment ac = (from c in link.AssignmentComments
                                    where c.commentID == commentID
                                    select c).First();
            MaterialComment mc = (from c in link.MaterialComments
                                  where c.commentID == commentID
                                  select c).First();
            LectureComment lc = (from c in link.LectureComments
                                 where c.commentID == commentID
                                 select c).First();

            if (cc != null)
                link.CourseComments.DeleteOnSubmit(cc);
            else if (ac != null)
                link.AssignmentComments.DeleteOnSubmit(ac);
            else if (mc != null)
                link.MaterialComments.DeleteOnSubmit(mc);
            else if (lc != null)
                link.LectureComments.DeleteOnSubmit(lc);

            link.Comments.DeleteOnSubmit(comm);
            save();
        }


        /// <summary>
        /// Saves changes to database
        /// Precondition:
        /// Postcondition: Database has been updated
        /// Author: Aron og Bjarmi
        /// Date: 03.05.2012
        /// </summary>
        public void save()
        {
            link.SubmitChanges();
        }


    }
}